package com.tuniu.agents.impl.base;

import com.tuniu.agents.agent.AgentManagerGroup;
import com.tuniu.agents.tools.flights.domestic.oneway.FlightLowPriceQueryTool;
import com.tuniu.agents.tools.flights.domestic.oneway.FlightTransferQueryTool;
import com.tuniu.agents.tools.flights.domestic.roundtrip.RoundFlightLowPriceQueryTool;
import com.tuniu.agents.tools.hotel.tool.NlowestPriceHotelList;
import com.tuniu.agents.tools.hotel.tool.PlowestPriceHotelList;
import com.tuniu.agents.tools.train.tool.SearchAllTrainTool;
import com.tuniu.agents.tools.train.tool.SearchConditionTrainTool;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class BaseAgentConfig {
    private final BaseAgentConfigProperties properties;

    public Map<String, String> selectionAgents;

    public Map<String, String> dispatcherAgents;

    public BaseAgentConfig(BaseAgentConfigProperties properties) {
        this.properties = properties;
        selectionAgents = new LinkedHashMap<>(properties.getSelections());
        dispatcherAgents = new LinkedHashMap<>(properties.getDispatcher());
    }

    /**
     * List of all Agents requiring routing
     */
    @Bean(Coordinator.AGENT_ID)
    public Coordinator generalTicketAgent(AgentManagerGroup agentManagerGroup) {
        Map<String, String> matchAgent = new LinkedHashMap<>(selectionAgents);
//        matchAgent.put(AgentConfig.OTHER_AGENT_ID, "Provide Q&A services.");
        return new Coordinator(agentManagerGroup, matchAgent);
    }

    @Bean(Dispatcher.AGENT_ID)
    public Dispatcher dispatcher(AgentManagerGroup agentManagerGroup) {
        return new Dispatcher(agentManagerGroup, dispatcherAgents);
    }

    /**
     * Follow-up question: matching tool names
     */
    public static final List<String> matchToolNames = List.of(
            FlightLowPriceQueryTool.TOOLNAME,
            FlightTransferQueryTool.TOOLNAME,
            RoundFlightLowPriceQueryTool.TOOLNAME,
            SearchAllTrainTool.TOOLNAME,
            SearchConditionTrainTool.TOOLNAME,
            NlowestPriceHotelList.TOOLNAME,
            PlowestPriceHotelList.TOOLNAME
    );

    @Bean(Analyst.AGENT_ID)
    public Analyst analyst(AgentManagerGroup agentManagerGroup, @Qualifier("qwen-32b") ChatOptions chatOptions) {
        return new Analyst(agentManagerGroup, matchToolNames, selectionAgents,chatOptions);
    }

}
